package com.atguigu.tingshu.order.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

@Configuration
public class PrivateKeyConfigInt {
    @Bean
    public RSAPrivateKey privateKey (){
        //资源初始化
        ClassPathResource classPathResource = new ClassPathResource("tingshu.jks");
        //读取私钥
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(classPathResource, "tingshu".toCharArray());
        //读取文件中的两把钥匙
        KeyPair keyPair = keyStoreKeyFactory.getKeyPair("tingshu", "tingshu".toCharArray());
        RSAPrivateKey aPrivate = (RSAPrivateKey)keyPair.getPrivate();
        //获取私钥，放进容器
        return aPrivate;
    }

    @Bean
    public RSAPublicKey publicKey(){
        //资源初始化
        ClassPathResource classPathResource = new ClassPathResource("tingshu.jks");
        //读取公钥
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(classPathResource, "tingshu".toCharArray());
        //读取文件中的两把钥匙
//        KeyPair keyPair = keyStoreKeyFactory.getKeyPair("tingshu", "tingshu".toCharArray());
        RSAPublicKey aPublic = (RSAPublicKey)keyStoreKeyFactory.getKeyPair("tingshu", "tingshu".toCharArray()).getPublic();
        //获取私钥，放进容器
        return aPublic;
    }
}